package com.slothought.rowgazer

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;


class GazerException extends Exception{ 
    GazerException(){	super()	}
    GazerException(String msg){	super(msg)	}
    
}

class QualifiedName{
	String catalog;
	String schema;
	String name;
    
	QualifiedName(String catalog, String schema, String name){
		this.catalog = catalog;
		this.schema = schema;
		this.name = name;
	}
		
	
    public String toString(){
        return catalog  + "." + schema + "." + name + ""
    }
    
    public int hashCode() {
        return new HashCodeBuilder(17, 31).
            append(catalog).
            append(schema).
            append(name).
            toHashCode();
    }

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj.getClass() != getClass())
            return false;

        QualifiedName qn = (QualifiedName) obj;
        return new EqualsBuilder().
            // if deriving: appendSuper(super.equals(obj)).
            append(catalog, qn.catalog).
            append(schema, qn.schema).
            append(name, qn.name).
            isEquals();
    }

}

class Request{
    private String tableName;
    private Map<String, String> fieldNames;
    private Map<String,Map<String,String>> filterValues;
    
    public String getTableName() {
        return tableName;
    }
    
    public Map<String, String> getFieldNames() {
        return fieldNames;
    }
    
    public Map<String,Map<String,String>> getFilterValues() {
        return filterValues;
    }
    
    
    Request(String argT, Map<String, String> argCs, Map<String,Map<String,String>> argVs){
        //      validation column indexes
        
        if (!(argCs.keySet().max()  + 1 == argCs.keySet().size() &&  argCs.keySet().min() == 0)){
            throw new GazerException("column indexes not in the form or [0..n]" );
        }
        
        
        //         validate filter value indexes
        
        def checkValueIndexes = argVs.every{ // argCs is external var
            def keyset = it.getValue().keySet()
            
            keyset.size() == argCs.keySet().size() &&
                    keyset.max() == argCs.keySet().max() &&
                    keyset.min() == argCs.keySet().min() 
        }
        if (!checkValueIndexes){
            throw new GazerException("filter value indexes not in the form of [0..n]");
        }
        
        this.tableName = argT;
        this.fieldNames = argCs;
        this.filterValues = argVs;
    }

    public String toString(){
        return "[" + tableName + "," + fieldNames + "," + filterValues + "]"
    }
    
}

class SchemaInfo{
    
    String dataSourceConfigName
    String jdbcCatalog
    String jdbcSchema
    
    boolean isStaticSchema
    boolean isStaticCatalog
    public String toString(){
        return "[" + dataSourceConfigName + "," + isReferencing + "]"
    }
    
}






class TableMetadata{
	QualifiedName tableQName;
    
    List<FieldSet> fieldsets = new ArrayList<FieldSet>()

    class FieldSet{
	    boolean hasIndex = false
	    Map<String, ReferentialConstraint> references = new HashMap<String, ReferentialConstraint>()
	    Set<String> fields = new HashSet<String>()
	    
	    public String toString(){
	        return "[" + hasIndex + "," + references + ", " + fields + "]"
	    }	
	    
	}
    
    class ReferentialConstraint{
        String tableTargetQName = ""
        Map<String, String> fieldMapping = new HashMap<String, String>()
        boolean isReferencing = false
        boolean hasIndex = false
        
        public String toString(){
            return "[" + tableTargetQName + "," + fieldMapping + ", " + isReferencing + "]"
        }
        
    }
    
    
    
    public String toString(){
        return "[" + tableQName + "," + fieldsets + "]"
    }	
}

class Result{
	TableMetadata tableMD

    public String toString(){
        return "[" + tableMD + "]"
    }	
    
}




